import { defineStore } from 'pinia';
import { getToken, removeToken, setToken } from '../utils/token-utils';
// import type { UserInfoState } from './interface';
import {ElMessage} from 'element-plus'
import {staticRoutes} from '@/router/routes'
import userInfoApi from '@/api/userInfo';
import type { RouteRecordRaw} from 'vue-router'

interface UserInfoModel{
  avatar:string,
  buttons:string[],
  name:string,
  roles:string[],
  routes:string[]
}
interface UserInfoState{
  token:string,
  userInfo:UserInfoModel,
  menuRoutes:RouteRecordRaw[]

}

//用于初始化个人信息
const userInfoInit= () =>({
  avatar:'',
  buttons:[],
  name:'',
  roles:[],
  routes:[]

})


/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfoStore = defineStore('userInfo', {

	state: (): UserInfoState => ({
    token: getToken() as string,
    userInfo: userInfoInit(),
    menuRoutes: []
  }),

	actions: {
    async login (username:string,password:string){
      try {
        let result = await userInfoApi.login({username,password});
        // console.log(result); 
        setToken(result.token) 
        this.token = result.token ; 
      } catch (error) {
        ElMessage.error('登录失败，请重试')
        return Promise.reject(error)
        
      }
    },
    async getInfo(){
      try {
        let result = await userInfoApi.getInfo()
        // console.log(result);
        this.userInfo = result //存储用户信息
        this.menuRoutes = staticRoutes
      } catch (error) {
        ElMessage.error('获取用户信息失败，请重试')
        return Promise.reject(error)
      }
      
    },
    async reset () {
      try {
        await userInfoApi.getlogout()
        // 删除local中保存的token
        removeToken()
        // 提交重置用户信息的mutation
        this.token = ''
        this.userInfo = userInfoInit()     
      } catch (error) {
        ElMessage.error('退出登录失败，请重试')
        return Promise.reject(error);
        
      }
    },
    
    // getInfo () {
      //   // return new Promise((resolve, reject) => {
        //   //   setTimeout(() => {
          //   //     this.name = 'admin'
          //   //     this.avatar = 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif'
          //   //     this.menuRoutes = staticRoutes
          //   //     resolve({name: this.name, avatar: this.avatar, token: this.token})
          //   //   }, 1000)
          //   // })
          // },

    // reset () {
    //   // 删除local中保存的token
    //   removeToken()
    //   // 提交重置用户信息的mutation
    //   this.token = ''
    //   this.name = ''
    //   this.avatar = ''
    // },
	},
});

  // login (username: string, password: string) {
  //   return new Promise((resolve, reject) => {
  //     setTimeout(() => {
  //       if (username==='admin' && password==='111111') {
  //         const token = 'token-atguigu'
  //         setToken(token)
  //         this.token = token
  //         resolve(token)
  //       } else {
  //         reject(new Error('用户名或密码错误!'))
  //         ElMessage.error('用户名或密码错误!')
  //       }
  //     }, 1000)
  //   })
  // },